﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using ToolRental.Client.SMvvm.Admin.AdminService;

namespace ToolRental.Client.SMvvm.Admin
{
    public class DeviceTypeServiceAgent : IDeviceTypeServiceAgent
    {
        #region GetDeviceTypeList
        public async Task<List<DeviceType>> GetDeviceTypeList(INotifyError notifier)
        {
            List<DeviceType> deviceTypeList = new List<DeviceType>();
            try
            {
                AdminService.ToolType[] toolTypeListFromService;
                using (var asc = new AdminServiceClient())
                {
                    toolTypeListFromService = await asc.GetToolTypesAsync();
                }
                foreach (var tt in toolTypeListFromService)
                {
                    deviceTypeList.Add(new DeviceType()
                    {
                        BrandName = tt.BrandName,
                        TypeName = tt.TypeName,
                        Id = tt.Id
                    });
                }
            }
            catch (EndpointNotFoundException e)
            {
                notifier.ErrorOccured("A kiszolgáló nem található", e);
            }
            return deviceTypeList;
        }
        #endregion

        #region AddDeviceType

        public async void AddDeviceType(string brandName, string typeName, INotifyError notifier)
        {
            try
            {
                using (var asc = new AdminServiceClient())
                {
                    await asc.AddToolTypeAsync(new ToolType()
                        {
                            BrandName = brandName,
                            TypeName = typeName
                        });
                }
            }
            catch (EndpointNotFoundException e)
            {
                notifier.ErrorOccured("A kiszolgáló nem található!", e);
            }
        }

        #endregion

    }
}
